package com.example.composeplayground

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.composeplayground.common.TokenUtils
import com.example.composeplayground.screens.LoginScreen
import com.example.composeplayground.screens.MainScreen
import com.example.composeplayground.states.AlertManager
import com.example.composeplayground.ui.theme.ComposePlaygroundTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposePlaygroundTheme {
                val navController = rememberNavController()
                var startDestination = "Main"
                val token = TokenUtils.getToken()
                if (token == null) {
                    startDestination = "Login"
                }
                NavHost(navController = navController, startDestination = startDestination) {
                    composable("Main") {
                        MainScreen(navController)
                    }
                    composable("Login") {
                        LoginScreen(navController)
                    }
                }
                val showDialog = AlertManager.isShow.value
                val dialogContent = AlertManager.content.value
                if (showDialog) {
                    AlertDialog(
                        onDismissRequest = {
                            AlertManager.hide()
                        },
                        title = { Text(text = "提示") },
                        text = { Text(text = dialogContent) },
                        confirmButton = {
                            Button(onClick = {
                                AlertManager.hide()
                            }) {
                                Text("确定")
                            }
                        }
                    )
                }
            }
        }
    }
}